package com.lin.zoo.dao;

import com.lin.zoo.dao.Interface.IDPark;
import com.lin.zoo.po.*;
import com.lin.zoo.util.DataBase;
import com.lin.zoo.po.Messege;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ParkDao implements IDPark {
    private Connection con;

    private PreparedStatement state;
    //查看园区信息
    @Override
    public Messege vPark(){

        try{
            con = DataBase.getCon();
            //获取连接，通过DateBase处理好的通道，连接至mysql数据。
            String sql = "SELECT * FROM park WHERE status= ?";
            //设定mysql语句
            state = con.prepareStatement(sql);
            //执行sql语句
            state.setString(1, "1");
            ResultSet rs = state.executeQuery();
            List<Park> list = new ArrayList<>();//使用集合承载输出的信息，不包括园区id，与是否开放
            while(rs.next()){
                list.add(new Park(rs.getString("name"),rs.getString("type"),rs.getString("introduction"),rs.getString("area"),rs.getString("address"),rs.getString("starttime"),rs.getString("endtime")));
            }
            return new Messege("园区信息读取成功",list.toString().replace(",",""));


        }//捕捉异常
        catch(ClassNotFoundException | SQLException e){
            System.out.println("数据库数据调用失败" + this.getClass().getName());
        }finally{
            try{
                DataBase.close(state, con);
            } catch(SQLException e){
                System.out.println("连接关闭失败" + this.getClass().getName());
            }
        }
        return new Messege("数据读取失败",null);
    }
    @Override
    //删除园区信息
    public void delPark(Object id){
        try{
            con=DataBase.getCon();
            state = con.prepareStatement("DELETE FROM park WHERE id=?");
            state.setObject(1,id);
            state.executeUpdate();
            DataBase.close(state, con);
        } catch (SQLException | ClassNotFoundException ex) {
            System.out.println("数据删除失败" + this.getClass().getName());
        }
    }
    @Override
    //修改名字
    public void setName(Object id,String name){
        try{
            con=DataBase.getCon();
            state = con.prepareStatement("update park set name=? WHERE id=?");//将修改的数据返回数据库
            state.setObject(1,name);
            state.setObject(2,id);
            state.executeUpdate();
            DataBase.close(state, con);

        }catch (SQLException | ClassNotFoundException ex) {
            System.out.println("数据修改错误" + this.getClass().getName());
        }
    }
    @Override
    //修改园区类型
    public void setType(Object id,String type){
        try{
            con=DataBase.getCon();
            state = con.prepareStatement("update park set type=? WHERE id=?");//将修改的数据返回数据库
            state.setObject(1,type);
            state.setObject(2,id);
            state.executeUpdate();
            DataBase.close(state, con);

        }catch (SQLException | ClassNotFoundException ex) {
            System.out.println("数据修改错误" + this.getClass().getName());
        }
    }
    @Override
    //修改园区介绍
    public void setIntroduce(Object id,String introduce){
        try{
            con=DataBase.getCon();
            state = con.prepareStatement("update park set introduction=? WHERE id=?");//将修改的数据返回数据库
            state.setObject(1,introduce);
            state.setObject(2,id);
            state.executeUpdate();
            DataBase.close(state, con);

        }catch (SQLException | ClassNotFoundException ex) {
            System.out.println("数据修改错误" + this.getClass().getName());
        }
    }
    @Override
    //修改园区面积
    public void setArea(Object id,String area){
        try{
            con=DataBase.getCon();
            state = con.prepareStatement("update park set area=? WHERE id=?");//将修改的数据返回数据库
            state.setObject(1,area);
            state.setObject(2,id);
            state.executeUpdate();
            DataBase.close(state, con);

        }catch (SQLException | ClassNotFoundException ex) {
            System.out.println("数据修改错误" + this.getClass().getName());
        }
    }
    @Override
    //修改园区地址
    public void setAddress(Object id,String address){
        try{
            con=DataBase.getCon();
            state = con.prepareStatement("update park set address=? WHERE id=?");
            state.setObject(1,address);
            state.setObject(2,id);
            state.executeUpdate();
            DataBase.close(state, con);

        }catch (SQLException | ClassNotFoundException ex) {
            System.out.println("数据修改错误" + this.getClass().getName());
        }
    }
    @Override
    //修改开始时间
    public void setStart(Object id,String startTime){
        try{
            con=DataBase.getCon();
            state = con.prepareStatement("update park set starttime=? WHERE id=?");
            state.setObject(1,startTime);
            state.setObject(2,id);
            state.executeUpdate();
            DataBase.close(state, con);

        }catch (SQLException | ClassNotFoundException ex) {
            System.out.println("数据修改错误" + this.getClass().getName());
        }
    }
    @Override
    //修改结束时间
    public void setEnd(Object id,String endTime){
        try{
            con=DataBase.getCon();
            state = con.prepareStatement("update park set endtime=? WHERE id=?");
            state.setObject(1,endTime);
            state.setObject(2,id);
            state.executeUpdate();
            DataBase.close(state, con);

        }catch (SQLException | ClassNotFoundException ex) {
            System.out.println("数据修改错误" + this.getClass().getName());
        }
    }
    @Override
    //修改开园状态
    public void setStatus(Object id,String status){
        try{
            con=DataBase.getCon();
            state = con.prepareStatement("update park set status=? WHERE id=?");
            state.setObject(1,status);
            state.setObject(2,id);
            state.executeUpdate();
            DataBase.close(state, con);

        }catch (SQLException | ClassNotFoundException ex) {
            System.out.println("数据修改错误" + this.getClass().getName());
        }
    }
 }


